/**
 * Created by xuelian on 2018/3/28.
 */
class BuycardRecordPanel extends BasePanel {

    private scroller: eui.Scroller;
    private itemGroup: eui.Group;

    private pageTool: PageTool;

    private list: any[];

    private timestamp: number = 0;
    private page: number = 1;

    public constructor() {
        super();
        this.skinName = "BuycardRecordPanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.page = 1;
        this.pageTool.setPage(this.page);

        gameManager.i.addEventListener(GameEventType.UPDATE_BUYCARD_LIST, this.updateFangka, this);

        this.pageTool.addEventListener(CommonEventType.CHANGED, this.onUpdatePage, this);
    }

    private onUpdatePage() {
        if (this.page != this.pageTool.page) {
            this.page = this.pageTool.page;

            let start: number = (this.page - 1) * GameConst.Count_Page_Record;
            let end: number = start + GameConst.Count_Page_Record - 1;
            gameManager.socketManager.send(601, {
                start: start,
                end: end,
                time: this.timestamp
            });
        }
    }

    updateFangka(data: any) {

        if (data.hasOwnProperty('zong_shu')) {
            this.pageTool.setScope(1, Math.ceil(+data.zong_shu / GameConst.Count_Page_Record));
        }

        let list: any[] = [];

        if (data.hasOwnProperty('return_data')) {
            list = data.return_data;
        }
        else {
            list = data;
        }

        if (!list) return;

        this.clean();

        if (this.timestamp == 0 && list.length > 0) {
            this.timestamp = list[0].time;
        }

        this.list = list;

        let item: BuycardRecordItem;
        for (let i: number = 0; i < list.length; i++) {
            item = new BuycardRecordItem();

            this.itemGroup.addChild(item);

            item.update(list[i]);
        }
    }

    show() {
        super.show();

        this.timestamp = 0;
        this.page = 1;
        this.pageTool.setPage(this.page);

        gameManager.socketManager.send(601, {
            start: 0,
            end: GameConst.Count_Page_Record - 1,
            time: 0
        });
    }

    clean() {
        this.scroller.stopAnimation();
        this.itemGroup.removeChildren();
        this.itemGroup.scrollV = 0;
    }
}